package org.budo.cmbc.direct.sdk.impl;

import java.util.HashMap;
import java.util.Map;

import org.budo.cmbc.direct.sdk.CmbcRybSdk;
import org.budo.cmbc.direct.sdk.entity.ryb.MELERybHisIncomeQueryRequest;
import org.budo.cmbc.direct.sdk.entity.ryb.MELERybHisIncomeQueryResponse;
import org.budo.cmbc.direct.sdk.entity.ryb.MELERybOpenCloseAccountRequest;
import org.budo.cmbc.direct.sdk.entity.ryb.MELERybOpenCloseAccountResponse;
import org.budo.cmbc.direct.sdk.entity.ryb.MELERybProdInfoQueryRequest;
import org.budo.cmbc.direct.sdk.entity.ryb.MELERybProdInfoQueryResponse;
import org.budo.cmbc.direct.sdk.entity.ryb.MELERybProdJnlQueryRequest;
import org.budo.cmbc.direct.sdk.entity.ryb.MELERybProdJnlQueryResponse;
import org.budo.cmbc.direct.sdk.entity.ryb.MEModifyCustRiskPerInfoRequest;
import org.budo.cmbc.direct.sdk.entity.ryb.MEModifyCustRiskPerInfoResponse;
import org.budo.cmbc.direct.sdk.entity.ryb.MEQryCustRiskPerInfoQuestionRequest;
import org.budo.cmbc.direct.sdk.entity.ryb.MEQryCustRiskPerInfoQuestionResponse;
import org.budo.cmbc.direct.sdk.entity.ryb.MEQryCustRiskPerInfoRequest;
import org.budo.cmbc.direct.sdk.entity.ryb.MEQryCustRiskPerInfoResponse;
import org.budo.cmbc.direct.sdk.entity.ryb.MERiskQuestionQryRequest;
import org.budo.cmbc.direct.sdk.entity.ryb.MERiskQuestionQryResponse;
import org.budo.cmbc.direct.sdk.entity.ryb.MERiskResultCommitRequest;
import org.budo.cmbc.direct.sdk.entity.ryb.MERiskResultCommitResponse;
import org.budo.cmbc.direct.sdk.entity.ryb.MERiskResultQryRequest;
import org.budo.cmbc.direct.sdk.entity.ryb.MERiskResultQryResponse;

/**
 * 如意宝接口
 */
public class CmbcRybSdkImpl extends AbstractCmbcDirectBankSdk implements CmbcRybSdk {
    @Override
    public MELERybProdInfoQueryResponse MELERybProdInfoQuery(MELERybProdInfoQueryRequest request) {

        Map<String, Object> dataMap = new HashMap<String, Object>();

        String responseJson = this.messeageSender().sendMessage("MELERybProdInfoQuery", dataMap, request.getChannelJnlNo());

        return new MELERybProdInfoQueryResponse(request.getChannelJnlNo(), responseJson);
    }

    @Override
    public MELERybOpenCloseAccountResponse MELERybOpenCloseAccount(MELERybOpenCloseAccountRequest request) {

        String[] fields = { "EProtocolAcNo", "ProdSubId", "SignType", "PwdResult", "RandJnlNo", "Random", "FundSubscribeType" };
        Map<String, Object> dataMap = this.fieldValueMap(request, fields);

        String responseJson = this.messeageSender().sendMessage("MELERybOpenAccount", dataMap, request.getChannelJnlNo());

        return new MELERybOpenCloseAccountResponse(request.getChannelJnlNo(), responseJson);
    }

    @Override
    public MERiskQuestionQryResponse MERiskQuestionQry(MERiskQuestionQryRequest request) {

        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("ActionCode", this.getMerchantNo() + "1003");

        String responseJson = this.messeageSender().sendMessage("MERiskQuestionQry", dataMap, request.getChannelJnlNo());

        return new MERiskQuestionQryResponse(request.getChannelJnlNo(), responseJson);
    }

    @Override
    public MERiskResultCommitResponse MERiskResultCommit(MERiskResultCommitRequest request) {

        request.setActionCode(this.getMerchantNo() + "1003");

        String[] fields = { "EProtocolAcNo", "ProdId", "ActionCode", "Result" };
        Map<String, Object> dataMap = this.fieldValueMap(request, fields);

        String responseJson = this.messeageSender().sendMessage("MERiskResultCommit", dataMap, request.getChannelJnlNo());

        return new MERiskResultCommitResponse(request.getChannelJnlNo(), responseJson);
    }

    @Override
    public MERiskResultQryResponse MERiskResultQry(MERiskResultQryRequest request) {

        request.setActionCode(this.getMerchantNo() + "1003");

        String[] fields = { "EProtocolAcNo", "ProdId", "ActionCode" };
        Map<String, Object> dataMap = this.fieldValueMap(request, fields);

        String responseJson = this.messeageSender().sendMessage("MERiskResultQry", dataMap, request.getChannelJnlNo());

        return new MERiskResultQryResponse(request.getChannelJnlNo(), responseJson);
    }

    @Override
    public MEQryCustRiskPerInfoQuestionResponse MEQryCustRiskPerInfoQuestion(MEQryCustRiskPerInfoQuestionRequest request) {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("RiskType", request.getRiskType());

        String responseJson = this.messeageSender().sendMessage("MEQryCustRiskPerInfoQuestion", dataMap, request.getChannelJnlNo());

        return new MEQryCustRiskPerInfoQuestionResponse(request.getChannelJnlNo(), responseJson);
    }

    @Override
    public MEModifyCustRiskPerInfoResponse MEModifyCustRiskPerInfo(MEModifyCustRiskPerInfoRequest request) {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("EProtocolAcNo", request.getEProtocolAcNo());
        dataMap.put("PerInfoAnswer", request.getPerInfoAnswer());

        String responseJson = this.messeageSender().sendMessage("MEModifyCustRiskPerInfo", dataMap, request.getChannelJnlNo());

        return new MEModifyCustRiskPerInfoResponse(request.getChannelJnlNo(), responseJson);
    }

    @Override
    public MEQryCustRiskPerInfoResponse MEQryCustRiskPerInfo(MEQryCustRiskPerInfoRequest request) {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("EProtocolAcNo", request.getEProtocolAcNo());

        String responseJson = this.messeageSender().sendMessage("MEQryCustRiskPerInfo", dataMap, request.getChannelJnlNo());

        return new MEQryCustRiskPerInfoResponse(request.getChannelJnlNo(), responseJson);
    }

    @Override
    public MELERybProdJnlQueryResponse MELERybProdJnlQuery(MELERybProdJnlQueryRequest request) {

        String[] fields = { "EProtocolAcNo", "ProdSubId", "BeginDate", "EndDate", "TransFlag", "BeginNumber", "QueryNumber" };
        Map<String, Object> dataMap = this.fieldValueMap(request, fields);

        String responseJson = this.messeageSender().sendMessage("MELERybProdJnlQuery", dataMap, request.getChannelJnlNo());

        return new MELERybProdJnlQueryResponse(request.getChannelJnlNo(), responseJson);
    }

    @Override
    public MELERybHisIncomeQueryResponse MELERybHisIncomeQuery(MELERybHisIncomeQueryRequest request) {

        String[] fields = { "EProtocolAcNo", "ProdSubId", "BeginDate", "EndDate", "BeginNumber", "QueryNumber" };
        Map<String, Object> dataMap = this.fieldValueMap(request, fields);

        String responseJson = this.messeageSender().sendMessage("MELERybHisIncomeQuery", dataMap, request.getChannelJnlNo());

        return new MELERybHisIncomeQueryResponse(request.getChannelJnlNo(), responseJson);
    }
}
